package com.shiny.widgetsdk.network.okhttp.response;

import android.os.Looper;
import com.shiny.widgetsdk.util.CallbackConstant;
import com.shiny.widgetsdk.network.okhttp.exception.OkHttpException;
import com.shiny.widgetsdk.network.okhttp.listener.DispatchListener;
import com.shiny.widgetsdk.network.okhttp.listener.DisposeDatatHandler;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import android.os.Handler;
import org.json.JSONObject;

import java.io.IOException;

public class CommJSonCallback implements Callback {

    private Handler mDeliveryHandler;
    private DispatchListener mListener;
    private Class<?> mClass;

    public CommJSonCallback(DisposeDatatHandler handler){
        this.mListener = handler.mDispatchListener;
        this.mClass = handler.cls;
        this.mDeliveryHandler = new Handler(Looper.getMainLooper());
    }


    @Override
    public void onFailure(final Call call,final IOException e) {
        mDeliveryHandler.post(new Runnable() {
            @Override
            public void run() {
                mListener.onFailure(new OkHttpException(CallbackConstant.RESULT_Constant.ERROR_NETWORK,e));
            }
        });
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        mDeliveryHandler.post(new Runnable() {
            @Override
            public void run() {
               handlerResponse(response);
            }
        });
    }


    private void handlerResponse(Response response) {
        if( null == response ) {
            mListener.onFailure(new OkHttpException(CallbackConstant.RESULT_Constant.ERROR_NETWORK,CallbackConstant.FieldsConstant.EMPTY_MSG));
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(response.toString());
            if(jsonObject.has(CallbackConstant.FieldsConstant.RESULT_CODE)) {
                if(jsonObject.getInt(CallbackConstant.FieldsConstant.RESULT_CODE) == CallbackConstant.FieldsConstant.RESULT_CODE_VALUE ){
                    if(mClass == null) {
                        mListener.onSuccess(jsonObject);

                    } else {
                        Object object = null;//ResponseEntityToModule.ParseJsonObjectToModule(jsonObject,mClass);
                        if (null == object) {
                            mListener.onFailure(new OkHttpException(CallbackConstant.RESULT_Constant.ERROR_JSON
                                    , CallbackConstant.FieldsConstant.EMPTY_MSG) );
                        } else {
                            mListener.onSuccess(object);
                        }
                    }

                } else {
                    mListener.onFailure(new OkHttpException(CallbackConstant.RESULT_Constant.ERROR_JSON
                            , jsonObject.getInt(CallbackConstant.FieldsConstant.RESULT_CODE)));
                }
            }

        }catch (Exception e) {
            mListener.onFailure(new OkHttpException(CallbackConstant.RESULT_Constant.ERROR_OTHER, e));
        }
        return;
    }
}
